package com.benmei.waijiaojun.activity;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.benmei.waijiaojun.R;
import com.benmei.waijiaojun.adapter.MyFragmentPagerAdapter;

import butterknife.BindView;

/**
 * 作者：husunxing
 * 创建日期：2017/11/27.
 * 邮箱：husunxing@outlook.com
 * 描述：主页面
 */

public class MainActivity extends BaseActivity {

    @BindView(R.id.tab_view_pager)
    ViewPager tabViewPager;
    @BindView(R.id.bottom_tab_layout)
    TabLayout bottomTabLayout;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;

    public int[] images = new int[]{R.drawable.tab_home_selector, R.drawable.tab_course_selector,
            R.drawable.tab_live_selector, R.drawable.tab_message_selector, R.drawable.tab_mine_selector};
    public String[] mTabTitle = new String[]{"首页", "名师班课", "知识市场", "消息", "我的"};

    private static boolean isExit = false;// 定义一个变量，来标识是否退出

    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initViews() {
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        tabViewPager.setAdapter(myFragmentPagerAdapter);
        bottomTabLayout.setupWithViewPager(tabViewPager);

        for (int i = 0; i < bottomTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = bottomTabLayout.getTabAt(i);
            tab.setCustomView(getTabView(this, i));
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {

    }

    public View getTabView(Context context, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.home_tab_content, null);
        ImageView tabIcon = (ImageView) view.findViewById(R.id.tab_content_image);
        tabIcon.setImageResource(images[position]);
        TextView tabText = (TextView) view.findViewById(R.id.tab_content_text);
        tabText.setText(mTabTitle[position]);
        tabText.setTextColor(bottomTabLayout.getTabTextColors());
        return view;
    }


    @Override
    public void onKeyBack() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            // 利用handler延迟发送更改状态信息
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }
}
